package work.timejay.common;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;
import work.timejay.constants.DateConstants;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 实体类基类
 *
 * @author wwj
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SuperModel<T extends SuperModel<?>> extends Model<T> {
    /**
     * 主键
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    @DateTimeFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    @DateTimeFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    private LocalDateTime updateTime;

    /**
     * 乐观锁版本号
     */
    @Version
    @TableField(fill = FieldFill.INSERT)
    private Long version;

    /**
     * 逻辑删除字段
     */
    @TableField(value = "del_state")
    private Boolean delState;

    /**
     * 删除时间
     */
    @TableField(value = "delete_time")
    @JsonFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    @DateTimeFormat(pattern = DateConstants.DATE_YYYY_MM_DD_HH_MM_SS_FORMAT)
    private LocalDateTime deleteTime;

    public static final String ID = "id";

    public static final String CREATE_TIME = "create_time";

    public static final String UPDATE_TIME = "update_time";

    public static final String VERSION = "version";

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}
